package com.example.lottery_system.service.activitystatus.operator;

import com.example.lottery_system.dao.DataObject.ActivityDO;
import com.example.lottery_system.dao.DataObject.ActivityPrizeDO;
import com.example.lottery_system.dao.Mapper.ActivityMapper;
import com.example.lottery_system.dao.Mapper.ActivityPrizeMapper;
import com.example.lottery_system.service.dto.DrawPrizeStatusDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@Slf4j
public class ActivityOperator extends AbstractActivityOperator{
    @Override
    public Integer sequence() {
        return 2;
    }

    @Autowired
    ActivityMapper activityMapper;

    @Autowired
    ActivityPrizeMapper activityPrizeMapper;
    @Override
    public Boolean needConvert(DrawPrizeStatusDto drawPrizeStatusDto) {
        if (null == drawPrizeStatusDto) {
            log.warn("needConvert::drawPrizeStatusDto为空!");
            return false;
        }

        if (null == drawPrizeStatusDto.getActivityId()
                || null == drawPrizeStatusDto.getPrizeId()
                || CollectionUtils.isEmpty(drawPrizeStatusDto.getUserIds())) {
            log.warn("needConvert::activityID或活动奖品相关的为空!");
            return false;
        }

        List<ActivityPrizeDO> activityPrizeDoList = activityPrizeMapper.selectByActivityId(drawPrizeStatusDto.getActivityId());
        if (CollectionUtils.isEmpty(activityPrizeDoList)) {
            return false;
        }

        for (ActivityPrizeDO activityPrizeDo: activityPrizeDoList) {
            if (activityPrizeDo.getStatus().equalsIgnoreCase("running")) {
                return false;
            }
        }

        ActivityDO activityDo = activityMapper.selectByActivityId(drawPrizeStatusDto.getActivityId());
        if (null == activityDo) {
            log.warn("needConvert查询到的activityDo为空!");
            return false;
        }



        return true;
    }

    @Override
    public Boolean convert(DrawPrizeStatusDto drawPrizeStatusDto) {

        try {
            activityMapper.updateStatus(drawPrizeStatusDto.getActivityId(),drawPrizeStatusDto.getTargetActivityStatus().name());
            return true;
        } catch (Exception e) {
            log.warn("活动状态数据库转化异常!");
            return false;
        }


    }
}
